package com.huaxiafinance.management.controller;


import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.huaxiafinance.management.dto.response.QueryRoleDto;
import com.huaxiafinance.management.mapper.RoleMapper;
import com.huaxiafinance.management.model.RolePermission;
import com.huaxiafinance.management.service.RolePermissionService;
import com.huaxiafinance.management.util.MessageResult;
import com.huaxiafinance.management.util.ServiceCode;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller("rolePermissionController")
@RequestMapping("/rp")
public class RolePermissionController {

  private static final Logger log = Logger.getLogger(RolePermissionController.class);

  @Autowired
  private RolePermissionService rolePermissionService;
  @Autowired
  private RoleMapper roleMapper;

  @RequestMapping(value = "/queryRolePermission", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String queryRolePermission(Integer id) throws Exception {

    log.info("角色权限查询请求参数" + id);

    MessageResult messageResult = new MessageResult();

    List<QueryRoleDto> list = rolePermissionService.queryRolePermission(id);
    messageResult.setData(list);
    messageResult.setParameter(roleMapper.selectisDesensitization(id));
    messageResult.setResultMessage(ServiceCode.SUCCESS);

    return messageResult.getAsJSON();
  }

  @RequestMapping(value = "/addRolePermission", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String addRolePermission(HttpServletRequest request) throws Exception {
    String json = request.getParameter("json");

    log.info("新增角色权限请求参数" + json);

    MessageResult messageResult = new MessageResult();

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    RolePermission rolePermission = mapper.readValue(json, RolePermission.class);

    boolean flag = rolePermissionService.addRolePermission(rolePermission);

    if (flag) {
      messageResult.setResultMessage(ServiceCode.SUCCESS);
    } else {
      messageResult.setResultMessage(ServiceCode.EXCEPTION_ERROR);
    }

    log.info("新增角色权限返回参数" + messageResult.getAsJSON());

    return messageResult.getAsJSON();
  }

  @RequestMapping(value = "/updateRolePermission", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String updateRolePermission(HttpServletRequest request) throws Exception {
    String json = request.getParameter("json");

    log.info("修改角色权限请求参数" + json);

    MessageResult messageResult = new MessageResult();

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    RolePermission rolePermission = mapper.readValue(json, RolePermission.class);

    boolean flag = rolePermissionService.updateRolePermission(rolePermission);

    if (flag) {
      messageResult.setResultMessage(ServiceCode.SUCCESS);
    } else {
      messageResult.setResultMessage(ServiceCode.EXCEPTION_ERROR);
    }

    log.info("修改角色权限返回参数" + messageResult.getAsJSON());

    return messageResult.getAsJSON();
  }

  @RequestMapping(value = "/delRolePermission", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String delRolePermission(Integer id) throws Exception {
    log.info("修改角色权限请求参数" + id);

    MessageResult messageResult = new MessageResult();

    boolean flag = rolePermissionService.delRolePermission(id);

    if (flag) {
      messageResult.setResultMessage(ServiceCode.SUCCESS);
    } else {
      messageResult.setResultMessage(ServiceCode.EXCEPTION_ERROR);
    }

    log.info("新增角色返回参数" + messageResult.getAsJSON());

    return messageResult.getAsJSON();
  }
}